<template>
  <div class="title-wrapper">
    <div class="route-record" v-show="isRouteRecordRef">
      <div
        :class="`Prev ${isPrev ? '' : 'disable'}`"
        @click="isPrev && goBack()"
      >
        <el-icon><ArrowLeft /></el-icon>
      </div>
      <div
        :class="`next ${isNext ? '' : 'disable'}`"
        @click="isNext && goForward()"
      >
        <el-icon><ArrowRight /></el-icon>
      </div>
    </div>
    <!-- 新建 -->
    <div class="settings">
      <el-popover
        placement="bottom"
        :width="200"
        trigger="hover"
        :teleported="false"
      >
        <template #reference>
          <el-button type="primary" class="addbtn" size="small" round
            >新建</el-button
          >
        </template>
        <div class="ecvents">
          <el-button
            :icon="Memo"
            class="btn"
            size="small"
            round
            @click="handleAdd('book')"
            >书签</el-button
          >
          <el-button
            :icon="Collection"
            class="btn"
            size="small"
            round
            @click="handleAdd('meon')"
            >便签</el-button
          >
        </div>
      </el-popover>
    </div>
    <!-- 模糊搜索 -->
    <el-popover
      placement="bottom"
      :width="500"
      :visible="visibleRef"
      :teleported="false"
    >
      <template #reference>
        <div class="search" @click="handleClickSearch">
          <img src="../assets/imgs/search.png" width="20" />
          <input
            type="text"
            placeholder="请输入"
            v-model="valueRef"
            @focus="
              () => {
                visibleRef = true;
                search();
              }
            "
            @blur="() => (visibleRef = false)"
          />
        </div>
      </template>
      <div class="contents">
        <div class="search-res">
          <label>书签</label>
          <ul v-if="searchListReadonly.length">
            <li
              v-for="(item, index) in searchListReadonly"
              :key="index"
              @click="handleClick(item, 'shuqian')"
            >
              <el-icon><Link /></el-icon>
              <span>{{ item.title }}</span>
            </li>
          </ul>
          <template v-else>
            <skeletonAnEmpty v-model:loading="searchInfoLoadingRendonly" description="暂无书签结果！" :image-size="50"/>
          </template>
        </div>
        <div class="search-res">
          <label>便签</label>
          <ul v-if="searchMenoListReadonly.length">
            <li
              v-for="(item, index) in searchMenoListReadonly"
              :key="index"
              @click="handleClick(item, 'bianqian')"
            >
              <el-icon><Link /></el-icon>
              <span>{{ item.name }}</span>
            </li>
          </ul>
          <template v-else>
            <skeletonAnEmpty v-model:loading="searchMenoLodingReadonly" description="暂无便签结果！" :image-size="50"/>
          </template>
        </div>
      </div>
    </el-popover>
  </div>
</template>
<script setup name="TopBar">
import { ref, inject, watch, computed } from 'vue';
import { Link, ArrowLeft, ArrowRight } from '@element-plus/icons-vue';
import { Memo, Collection } from '@element-plus/icons-vue';
import { useRouter, useRoute } from 'vue-router';
import useSymbols from '../compostions/useSymbols';
import skeletonAnEmpty from '../components/skeletonAnEmpty/index.vue'

const router = useRouter();
const route = useRoute();

const positionRef = ref(0);
const isRouteRecordRef = ref(false);
const {
  setShowDialog,
  searchListReadonly,
  searchMenoListReadonly,
  searchInfoLoadingRendonly,
  searchMenoLodingReadonly,
  searchInfoList,
  searchMenoList,
} = inject(useSymbols);

const handleClickSearch = () => {};

const search = () => {
  searchInfoList(valueRef.value);
  searchMenoList(valueRef.value);
};

const valueRef = ref('');
watch(valueRef, (newValue) => {
  search();
});

// 路由前进、后退
const goBack = () => {
  router.back();
};

const goForward = () => {
  router.forward();
};

const hasRouteStack = () => {
  return router.currentRoute.value.matched.length > 1;
};
watch(route, async (newRoute) => {
  positionRef.value = window.history.state?.position || 0;
  isRouteRecordRef.value = hasRouteStack();
});

// 计算路由记录中还有没有上一条
const isPrev = computed(() => {
  return positionRef.value === 0 ? false : true;
});
// 计算路由记录中还有没有下一条
const isNext = computed(() => {
  return positionRef.value === window.history.length - 1 ? false : true;
});

// 添加标签或便签
const handleAdd = (name) => {
  switch (name) {
    case 'book':
      router.push('/index/book');
      setTimeout(() => {
        // 显示新增弹窗
        setShowDialog(true);
      }, 50);
      break;
    default:
      router.push('/index/memoEdit');
      break;
  }
};

const visibleRef = ref(false);
// 打开链接
const handleClick = (item, type) => {
  switch (type) {
    case 'shuqian':
      window.myApi
        ? window.myApi.sendOpenSellUrl(item.url)
        : window.open(item.url);
      break;
    case 'bianqian':
      router.push({ path: '/index/memoEdit', query: { id: item.id } });
      break;
  }
};
</script>
<style scoped lang="less">
.title-wrapper {
  --search-input-bg: #f8f7fe;
  --search-input-border-color: #f7f6fc;
  --text-color: #7b7b7b;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  /deep/ .el-popper {
    border-radius: 15px;
    .contents {
      display: grid;
      grid-template-columns: repeat(2, 225px);
      grid-column-gap: 15px;
      .search-res {
        label {
          font-size: 14px;
          color: #333;
          font-weight: bold;
          display: block;
          margin-bottom: 6px;
          transform: translateX(8px);
        }
        ul {
          margin: 0;
          padding: 0;
          min-height: 30px;
          max-height: 400px;
          overflow-y: auto;
          /* 隐藏滚动条的样式 */
          &::-webkit-scrollbar {
            display: none;
          }
          li {
            padding: 2px 4px;
            display: flex;
            color: var(--system-primary-color);
            cursor: pointer;
            position: relative;

            &:hover {
              font-weight: bold;
              span {
                &::before {
                  content: '';
                  position: absolute;
                  left: -18px;
                  bottom: -2px;
                  background-color: var(--system-primary-color);
                  height: 1px;
                  animation: myWidth 0.4s forwards;
                }
              }
            }
            span {
              width: calc(100% - 18px);
              display: block;
              font-size: 12px;
              margin-left: 6px;
              position: relative;
            }
          }
        }
      }
    }

    .ecvents {
      padding: 10px 0;
      text-align: center;
      .btn {
        background-color: var(--system-primary-color);
        color: #fff;
        border: none;
      }
    }
  }
  .route-record {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: center;
    .routeButton() {
      display: flex;
      color: #888;
      padding: 4px 8px;
      background-color: #f5f5f5;
      font-size: 15px;
      cursor: pointer;
      &:hover {
        filter: brightness(0.98);
      }
    }
    .Prev {
      .routeButton();
      border-top-left-radius: 999px;
      border-bottom-left-radius: 999px;
    }
    .next {
      .routeButton();
      border-top-right-radius: 999px;
      border-bottom-right-radius: 999px;
    }
    .disable {
      opacity: 0.6;
      cursor: not-allowed;
    }
  }
  .settings {
    flex: 30;
    display: flex;
    align-items: center;
    justify-content: end;
    margin-right: 40px;
    .setting-item {
      padding: 4px 16px;
      color: #666;
    }
  }
  .search {
    display: flex;
    align-items: center;
    flex: 10;
    border: 1px solid var(--search-input-border-color);
    background-color: var(--search-input-bg);
    border-radius: 14px;
    padding-left: 8px;
    input {
      line-height: 30px;
      background: none;
    }
  }
  .addbtn {
    background-color: var(--system-primary-color);
    border: none;
  }
}
@keyframes myWidth {
  0% {
    width: 0px;
  }
  100% {
    width: calc(100% + 19px);
  }
}
</style>
